package cn.dnui_zzh807.dnuifood_16110100807.controller;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnui_zzh807.dnuifood_16110100807.R;
import cn.dnui_zzh807.dnuifood_16110100807.SomethingElse.InsertComments;
import cn.dnui_zzh807.dnuifood_16110100807.SomethingElse.Purchase;
import cn.dnui_zzh807.dnuifood_16110100807.adapter.CommentAdapter;
import cn.dnui_zzh807.dnuifood_16110100807.bean.CommentInfo;
import cn.dnui_zzh807.dnuifood_16110100807.bean.FoodInfo;
import cn.dnui_zzh807.dnuifood_16110100807.common.Constants;
import cn.dnui_zzh807.dnuifood_16110100807.listener.OnRetrofitListener;
import cn.dnui_zzh807.dnuifood_16110100807.model.CollectModel;
import cn.dnui_zzh807.dnuifood_16110100807.model.FoodModel;
import cn.dnui_zzh807.dnuifood_16110100807.service.FoodService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class FoodActivity extends AppCompatActivity implements OnRetrofitListener<FoodInfo> {
    private Menu menu;
    private FoodInfo foodInfo;
    private RecyclerView recyclerView;
    private String user_id,id,foodname,food_id,collect,foodprice;
    private ImageButton collect_1,collect_2;
    private Button button,purchase;
    private TextView foodname1,intro,price;
    private ImageView pic;
    private CommentAdapter commentAdapter;

    public FoodActivity(){

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState);
        setContentView(R.layout.food_adapter);
        SharedPreferences sharedPreferences = getSharedPreferences("zzh",MODE_PRIVATE);
         user_id = sharedPreferences.getString("user_id","user_id");
        id = getIntent().getStringExtra("shop_id");
        foodname = getIntent().getStringExtra("foodname");
        food_id = getIntent().getStringExtra("food_id");
        Toast.makeText(FoodActivity.this,food_id,Toast.LENGTH_SHORT).show();
        initData(food_id);

    }

    void initData(String food_id){
        FoodModel foodModel = new FoodModel();
        foodModel.getFoodById(food_id,this);//访问服务器请求

    }

    @Override
    public void onSuccess(FoodInfo foodInfo,int flag) {
        this.foodprice=foodInfo.getPrice();
        this.foodInfo=foodInfo;
       initViews();
       initData1();

    }

    @Override
    public void onFail() {
        Toast.makeText(FoodActivity.this,"网络连接失败",Toast.LENGTH_SHORT).show();
    }

    //生成Menu的对象
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //创建菜单
        getMenuInflater().inflate(R.menu.food_list_collect,menu);
         this.menu=menu;
        //菜单创建完毕后向服务器发送请求，判断是否已经收藏
        menu.findItem(R.id.item_collect1).setTitle(foodname);
       isCollected(menu);
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //实现"显示收藏还是取消收藏"
        switch (item.getItemId()){
            case R.id.item_collect:
                String title = item.getTitle().toString();
                if (title.equals("收藏"))
                {
                    //收藏
                    collectFood(menu);
                    collect_1.setImageResource(R.drawable.ic_search_red);
                }
                else
                {
                    //取消收藏
                    uncollectFood(menu);
                    collect_1.setImageResource(R.drawable.ic_shoucahng);
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }
    private void initData1(){
        foodname1.setText(foodInfo.getFoodname());
        price.setText(foodInfo.getPrice()+"元");
        intro.setText(foodInfo.getIntro());
        Picasso.get().load(Constants.BASE_URL+foodInfo.getPic()).into(pic);
        collect_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (menu.findItem(R.id.item_collect).getTitle().equals("收藏"))
                {
                    //收藏
                    collectFood(menu);
                    collect_1.setImageResource(R.drawable.ic_search_red);
                }
                else
                {
                    //取消收藏
                    uncollectFood(menu);
                    collect_1.setImageResource(R.drawable.ic_shoucahng);
                }
            }
        });

        collect_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String strfood_id = food_id;
                Intent intent = new Intent(FoodActivity.this,InsertComments.class);
                intent.putExtra("food_id",strfood_id);
                startActivity(intent);
            }
        });


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getAllCommentsByFood(food_id);
            }
        });

        purchase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FoodActivity.this,Purchase.class);
                intent.putExtra("user_id",user_id);
                intent.putExtra("food_id",food_id);
                intent.putExtra("price",foodInfo.getPrice());
                startActivity(intent);
            }
        });

    }

    private void initViews(){
        foodname1=(TextView) findViewById(R.id.collect_name);
        intro=(TextView) findViewById(R.id.food_intro1);
        pic=(ImageView)findViewById(R.id.food_pic1);
        price=(TextView) findViewById(R.id.price1);
        collect_1= findViewById(R.id.collect_button);
        collect_2=findViewById(R.id.imageButton2);
        button=findViewById(R.id.button4);
        purchase=findViewById(R.id.purchase);
        recyclerView = (RecyclerView)findViewById(R.id.food_comment);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    private void isCollected(Menu menu){
        String food_id = getIntent().getStringExtra("food_id");
        SharedPreferences sharedPreferences = getSharedPreferences("ljp",MODE_PRIVATE);
        String user_id = sharedPreferences.getString("user_id","user_id");
        CollectModel collectModel = new CollectModel();
        collectModel.isCollect(user_id,food_id,"1",menu);
    }

    private void collectFood(Menu menu){
        String food_id = getIntent().getStringExtra("food_id");
        SharedPreferences sharedPreferences = getSharedPreferences("ljp",MODE_PRIVATE);
        String user_id = sharedPreferences.getString("user_id","user_id");
        CollectModel collectModel = new CollectModel();
        collectModel.userCollectFood(user_id,food_id,menu);

    }

    private void uncollectFood(Menu menu){
        String food_id = getIntent().getStringExtra("food_id");
        SharedPreferences sharedPreferences = getSharedPreferences("ljp",MODE_PRIVATE);
        String user_id = sharedPreferences.getString("user_id","user_id");
        CollectModel collectModel = new CollectModel();
        collectModel.unCollectFood_01(user_id,food_id,menu);
    }

    public void getAllCommentsByFood(String food_id){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        FoodService foodService = retrofit.create(FoodService.class);
        Call<List<CommentInfo>> call = foodService.getAllCommentsByFood(food_id);
        Callback<List<CommentInfo>> callback = new Callback<List<CommentInfo>>() {
            @Override
            public void onResponse(Call<List<CommentInfo>> call, Response<List<CommentInfo>> response) {
               // Toast.makeText(FoodActivity.this,"网络成功",Toast.LENGTH_SHORT).show();
                      List<CommentInfo> commentsInfo =  response.body();
                      setCommentAdapter(commentsInfo);
            }

            @Override
            public void onFailure(Call<List<CommentInfo>> call, Throwable t) {
            Toast.makeText(FoodActivity.this,"网络失败",Toast.LENGTH_SHORT).show();
            }
        };
        call.enqueue(callback);
    }


    private void setCommentAdapter(List<CommentInfo> commentsInfo){
        commentAdapter=new CommentAdapter(this,commentsInfo);
        recyclerView.setAdapter(commentAdapter);
    }


}

